home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9083 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.0 KB  |  125 lines

  1. Path: gail.ripco.com!mambuhl
  2. From: mambuhl@ripco.com (Martin Ambuhl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: separate compilation
  5. Date: 7 Mar 1996 23:51:09 GMT
  6. Organization: Ripco Communications, Inc.
  7. Message-ID: <4hnspd$b2v@gail.ripco.com>
  8. NNTP-Posting-Host: golden.ripco.com
  9.  
  10. D.C.Molero@dcs.warwick.ac.uk (Daniel Castillo Molero)
  11. in <1996Mar2.164559.24931@dcs.warwick.ac.uk> writes:
  12.  
  13. >My problem is the following:
  14. >I had a program which creates a linked list and then prints it, and was
  15. >working, but when I tried to separate it in 3 different files, and compile
  16. >it using
  17. >           gcc file1.c file2.c file3.c
  18.  
  19. >it doesn't compile.
  20.  
  21. 1) Your main() in file1.c never calls the functions in file2.c
  22. or file3.c, so it could never have been working even before splitting
  23. into separate files.
  24.  
  25. 2) You need to reread your C language reference on `extern'.  It does
  26. not mean "go search the disk for the text need to complete compilation."
  27. In the following revision of your code, note the use of a new file
  28. (called files.h) to accomplish what you are trying to do.
  29.  
  30. In the following revision of your code, my changes and comments are, as
  31. usual, marked with `/* mha - ... */'.
  32.  
  33.  
  34.  /* mha - files.h (new file)  */
  35. struct side {
  36.     int a;
  37.     struct side *next;
  38. };
  39.  
  40.  /* mha - file1.c */
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include "files.h"              /* mha - added */
  44.  
  45.  /* mha - removed useless `extern struct side;' */
  46.  
  47. int main(void)
  48. {
  49.     struct side *tmp;
  50.  
  51.     /* first_side is declared in file2.c and points to the first
  52.      * element of the list. */
  53.     /* mha - well, it had function scope in file1.c and was invisible
  54.      * to the following declaration */
  55.     extern struct side *first_side;
  56.     /* print the three elements */
  57.  
  58.     /* mha - note that the following, without at least initializing tmp
  59.      * to be NULL, may well result in a segfault (or equivalent) */
  60.     for (tmp = first_side; tmp; tmp = tmp->next)
  61.         printf("side: %d\n", tmp->a);
  62.  
  63.     return 0;
  64. }
  65.  
  66.  /* mha - file2.c */
  67. #include <stdlib.h>             /* mha - added.  Without this malloc()
  68.                                  * has an implicit declaration that,
  69.                                  * among other things, says malloc()
  70.                                  * returns an `int' rather than a `void
  71.                                  * *' */
  72. #include "files.h"              /* mha - added (definition of struct
  73.                                  * side move to files.h) */
  74. struct side *first_side;        /* mha - moved from within
  75.                                  * `create_list()', where it had
  76.                                  * function scope and was not visible
  77.                                  * to file1.c */
  78.  
  79. void store_side(struct side *, struct side **); /* mha - removed useless
  80.                                                  * variable names
  81.                                                  * [optional] */
  82.  
  83. void create_list(void)
  84. {
  85.     struct side *i;
  86.     /* mha - moved `struct side *first_side;' out of function scope */
  87.     /* mha - I have _not_ checked to see what this code actually does.
  88.      * I doubt that it does what you want, but that's up to you. */
  89.  
  90.     /* insert first element */
  91.     first_side = 0;
  92.     i = malloc(1 * sizeof(struct side));
  93.     i->a = 0;
  94.     store_side(i, &first_side);
  95.  
  96.     /* insert second element */
  97.     i = malloc(1 * sizeof(struct side));
  98.     i->a = 1;
  99.     store_side(i, &first_side);
  100.  
  101.     /* insert third elemenet */
  102.     i = malloc(1 * sizeof(struct side));
  103.     i->a = 2;
  104.     store_side(i, &first_side);
  105.  
  106. }
  107.  
  108.  /* mha - file3.c */
  109. #include "files.h"              /* mha - added */
  110.  
  111.  /* mha - removed useless `extern struct side;' */
  112.  
  113. /* insert a new element in the list */
  114.  /* mha - changed `first_side' to `s'.  Using the name of a global
  115.   * variable as the name of a parameter can confuse humans. */
  116. void store_side(struct side * i, struct side ** s)
  117. {
  118.     i->next = *s;
  119.     *s = i;
  120. }
  121.                                                                      
  122. --
  123. * Martin Ambuhl       net: mambuhl@ripco.com
  124. * Chicago, IL (USA)    
  125.